﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class FlowManagement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormItem)
            {
                UxFlow uxFlow = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxFlow;
                var flowUpdate = uxFlow.GetData();
                var flow = this.DbContext.VIBA_LUONGs.FirstOrDefault(u => u.Luong_Id.ToLower() == flowUpdate.Luong_Id.ToLower());
                if (flow != null)
                {
                    flow.Luong_Id = flowUpdate.Luong_Id;
                    flow.Luong_Ten = flowUpdate.Luong_Ten;
                    flow.Luong_Id_Tram_Den = flowUpdate.Luong_Id_Tram_Den;
                    flow.Luong_Id_Tram_Di = flowUpdate.Luong_Id_Tram_Di;
                    flow.Luong_Port_Den = flowUpdate.Luong_Port_Den;
                    flow.Luong_Port_Di = flowUpdate.Luong_Port_Di;
                    flow.Luong_Lich_Su_Duong_Di = flowUpdate.Luong_Lich_Su_Duong_Di;
                    flow.Luong_GhiChu = flowUpdate.Luong_GhiChu;
                    flow.Status = flowUpdate.Status;
                    flow.Luong_ThietBi_Di = flowUpdate.Luong_ThietBi_Di;
                    flow.Luong_ThietBi_Den = flowUpdate.Luong_ThietBi_Den;
                    flow.Luong_Card_Di = flowUpdate.Luong_Card_Di;
                    flow.Luong_Card_Den = flowUpdate.Luong_Card_Den;

                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = this.DbContext.VIBA_LUONGs.ToList();
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var flowId = (e.Item as GridDataItem)["Luong_Id"].Text;
                var flow = this.DbContext.VIBA_LUONGs.FirstOrDefault(u => u.Luong_Id.ToLower() == flowId.ToLower());
                if (flow != null)
                {
                    this.DbContext.VIBA_LUONGs.DeleteOnSubmit(flow);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var flow = e.Item.DataItem as VIBA_LUONG;

            //insert new station
            if (e.Item is GridEditFormInsertItem)
            {
                UxFlow uxFlow = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxFlow;
                uxFlow.AddComboBoxItems();
                uxFlow.AddComboBoxItems_TramDen();
                uxFlow.AddComboBoxItems_ThietbiDi();
                uxFlow.AddComboBoxItems_ThietbiDen();
                uxFlow.AddComboBoxItems_CardDi();
                uxFlow.AddComboBoxItems_CardDen();
                uxFlow.AddComboBoxItems_PortDi();
                uxFlow.AddComboBoxItems_PortDen(); 
                uxFlow.EnableFieldCode(true);
                uxFlow.InsertOrUpdate = true;
                
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UxFlow uxFlow = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxFlow;
                
                uxFlow.AddComboBoxItems();
                uxFlow.AddComboBoxItems_TramDen();
                uxFlow.AddComboBoxItems_ThietbiDi(flow.Luong_Id_Tram_Di);
                uxFlow.AddComboBoxItems_ThietbiDen(flow.Luong_Id_Tram_Den);
                uxFlow.AddComboBoxItems_CardDi(flow.Luong_ThietBi_Di);
                uxFlow.AddComboBoxItems_CardDen(flow.Luong_ThietBi_Den);
                uxFlow.AddComboBoxItems_PortDi(flow.Luong_Card_Di.ToString());
                uxFlow.AddComboBoxItems_PortDen(flow.Luong_Card_Den.ToString());
                uxFlow.EnableFieldCode(true);
                uxFlow.BindData(flow);
                uxFlow.InsertOrUpdate = false;                
            }

            if (e.Item is GridDataItem) 
            {
                var dataItem = e.Item as GridDataItem;
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);
                dataItem["TenTramDi"].Text = GetStationName(flow.Luong_Id_Tram_Di);
                dataItem["TenTramDen"].Text = GetStationName(flow.Luong_Id_Tram_Den);
                dataItem["TenThietbiDi"].Text =GetServiceName(flow.Luong_ThietBi_Di);
                dataItem["TenCardDi"].Text = GetCardName(flow.Luong_Card_Di.ToString());
                dataItem["TenThietbiDen"].Text = GetServiceName(flow.Luong_ThietBi_Den);
                dataItem["TenCardDen"].Text = GetCardName(flow.Luong_Card_Den.ToString());
            }
        }

        public string GetStationName(string stationId)
        {
            if (stationId == null)
                stationId = "";
            var st = this.StationList.FirstOrDefault(x => x.Tram_Id.Trim() == stationId.Trim());
            if (st != null)
                return st.Tram_Ten;
            return string.Empty;
        }

        public string GetServiceName(string serviceId)
        {
            if (serviceId == null)
                serviceId = "";
            var st = this.ServiceList.FirstOrDefault(x => x.ThietBi_Id.Trim() == serviceId.Trim());
            if (st != null)
                return st.ThietBi_Ten;
            return string.Empty;
        }

        public string GetCardName(string cardId)
        {
            if (cardId == null)
                cardId = "";
            var st = this.CardList.FirstOrDefault(x => x.Card_Id.ToString().Trim() == cardId.Trim());
            if (st != null)
                return st.Card_TenCard;
            return string.Empty;
        }

        private List<VIBA_TRAM> _stationList;
        public List<VIBA_TRAM> StationList
        {
            get
            {
                if (_stationList == null)
                    _stationList = new BTSModelDataContext().VIBA_TRAMs.ToList();
                return _stationList;
            }
            set
            {
                _stationList = value;
            }
        }

        private List<VIBA_THIET_BI> _serviceList;
        public List<VIBA_THIET_BI> ServiceList
        {
            get
            {
                if (_serviceList == null)
                    _serviceList = new BTSModelDataContext().VIBA_THIET_BIs.ToList();
                return _serviceList;
            }
            set
            {
                _serviceList = value;
            }
        }

        private List<VIBA_CARD> _cardList;
        public List<VIBA_CARD> CardList
        {
            get
            {
                if (_cardList == null)
                    _cardList = new BTSModelDataContext().VIBA_CARDs.ToList();
                return _cardList;
            }
            set
            {
                _cardList = value;
            }
        }

        private string GetStatus(string status)
        {
            return status == "1" ? "<font color='blue'>Hoạt động</font>" : "<font color='red'>Không hoạt động</font>";
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UxFlow uxFlow = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxFlow;
                var flow = uxFlow.GetData();
                if (!IsExists(flow.Luong_Id))
                {
                    uxFlow.SetErrorStatus(string.Empty);
                    this.DbContext.VIBA_LUONGs.InsertOnSubmit(flow);
                    this.DbContext.SubmitChanges();
                }
                else
                {
                    uxFlow.SetErrorStatus("Xin lỗi, mã luồng đã tồn tại.");
                    e.Canceled = true;
                }
            }
        }

        private bool IsExists(string flowId)
        {
            using (var ctx = new BTSModelDataContext())
            {
                var item = ctx.VIBA_LUONGs.FirstOrDefault(x => x.Luong_Id.ToLower() == flowId.ToLower());
                return item != null;
            }
        }
    }
}